package com.quarkus.redis.controller;

import com.quarkus.redis.entity.Project;
import com.quarkus.redis.service.QsRedisService;
import org.jboss.logging.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/projects")
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class QsRedisController {

    private static final Logger LOGGER = Logger.getLogger(QsRedisController.class);

    @Inject
    private QsRedisService qsRedisService;

    //获取一个主键值对象并提交Service服务对象获取该主键的Project对象
    @GET
    @Path("/get/{key}")
    public Project get(@PathParam("key") String key) {
        return new Project(key, qsRedisService.get(key));
    }

    //获取一个Project对象并提交Service服务对象增加一个Project对象
    @POST
    @Path("/put")
    public Project add(Project project) {
        LOGGER.info("获取一个Project对象并提交Service服务对象增加一个Project对象");
        qsRedisService.add(project.name, project.description);
        return project;
    }

    @GET
    @Path("/add/set/redis")
    public Project add() {
        LOGGER.info("获取一个Project对象并提交Service服务对象增加一个Project对象");
        Project project = new Project("1" , "zs");
        qsRedisService.add(project.name, project.description);
        return project;
    }

    //获取一个Project对象并提交Service服务对象并修改一个Project对象
    @PUT
    @Path("/{key}")
    public void update(Project project) {
        qsRedisService.update(project.name, project.description);
    }

    //获取一个主键值对象并提交Service服务对象并删除该主键的Project对象
    @DELETE
    @Path("/{key}")
    public void delete(@PathParam("key") String key) {
        qsRedisService.del(key);
    }
}
